package com.itmate.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.itmate.entity.Admins;
import com.itmate.service.AdminsService;


/**
 * Servlet implementation class DoAdminsLogin
 */
@WebServlet("/doadminslogin")
public class DoAdminsLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoAdminsLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String error="";
		String admin_id =request.getParameter("admin_id");
		System.out.println(admin_id);
		String admin_password =request.getParameter("admin_password");
		int admin_id2=Integer.parseInt(admin_id);
		
		Admins mu=new Admins();

	
		mu.setAdmin_id(admin_id2);
		mu.setAdmin_password(admin_password);
		mu.setAdmin_power("1");
		AdminsService mus =new AdminsService();
		
		if(mus.login(mu)) {
			HttpSession session = request.getSession();
			session.setAttribute("username", admin_id);
			session.setAttribute("power", mu.getAdmin_power());
			System.out.println("登录成功");
			request.setAttribute("username",mu.getAdmin_id());
			request.setAttribute("power", mu.getAdmin_power());
			request.getRequestDispatcher("/WEB-INF/jsp/Homepage.jsp").forward(request, response);
		}else {
			System.out.print(admin_id+"密码错误");
			error="密码错误";
			request.setAttribute("error",error);
			request.getRequestDispatcher("/WEB-INF/jsp/Error.jsp").forward(request, response);
		}
	}
	}


